package com.jzx.dao;

import com.jzx.bean.Information;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author JunedQQ
 * @create 2019/6/24
 * @since 1.0.0
 */
@Repository
public interface InfoDao {

    /**
     * 获取所有的资讯
     * @return
     */
    @Select("select * from info")
    List<Information> getAllInfo();

    /**
     * 根据id获取资讯信息
     * @param id
     * @return
     */
    @Select("select * from info where id = #{id}")
    Information getInfoById(String id);

    /**
     * 查找用户id所发布的资讯
     * @param userId
     * @return
     */
    @Select("select * from info where user_id = #{userId}")
    List<Information> getInfoByUserId(String userId);

    /**
     * 根据资讯id来获取查看量
     * @param id
     * @return
     */
    @Select("select see from info where id = #{id}")
    int getInfoSeeById(String id);

    /**
     * 根据id来获取资讯的标题
     * @param id
     * @return
     */
    @Select("select title from info where id = #{id}")
    String getInfoTitleById(String id);

    /**
     * 根据id来获取图片地址
     * @param id
     * @return
     */
    @Select("select address from info where id = #{id}")
    String getInfoPhotoAddressById(String id);

    /**
     * 根据id来获取用户id
     * @param id
     * @return
     */
    @Select("select user_id from info where id = #{id}")
    String getUserIdById(String id);

    /**
     * 添加资讯
     * @param info
     * @return
     */
    @Insert("insert into info(user_id,title,address) values(info.user.id,user.title,address)")
    int addInformation(Information info,@Param("address") String address);


    /**
     * 根据id来删除资讯
     * @param id
     * @return
     */
    @Delete("delete from info where id = #{id}")
    int deleteInfoById(String id);


    /**
     * 删除用户id所发布的所有资讯
     * @param userId
     * @return
     */
    @Delete("delete from info where user_id = #{userId}")
    int deleteInfoByUserId(String userId);

    /**
     * 根据id来更新userId
     * @param id
     * @param userId
     * @return
     */
    @Update("update info set user_id = #{userId} where id = #{id}")
    int updateInfoUserIdById(@Param("id") String id,@Param("userId") String userId);

    /**
     * 资讯的查看量加一
     * @param id
     * @return
     */
    @Update("update info set see = see+1 where id = #{id}")
    int infoSeePlusOne(String id);

    /**
     * 根据id来更新资讯的标题
     * @param id
     * @return
     */
    @Update("update info set title = #{title} where id = #{id}")
    int updateInfoTitleById(@Param("id") String id,@Param("title") String title);

    /**
     * 根据id来更新资讯的图片地址
     * @param id
     * @return
     */
    @Update("update info set address = #{address} where id = #{id}")
    int updateInfoPhotoAddressById(@Param("id") String id,@Param("address") String address);

    /**
     * 获取十个资讯
     * @param start 起始位置
     * @return
     */
    @Select("select * from info limit #{start},10")
    List<Information> getTenInfo(int start);


    /**
     * 获取任意资讯
     * @param start 起始位置
     * @param count 获取个数
     * @return
     */
    @Select("select * from info limit #{start},#{count}")
    List<Information> getAnyInfo(int start,int count);

}